/*
 *
 * C++ MaJiang for 血流红中
 *
 * TuyooGame, 2021
 * zhouxin@tuyoogame.com
 *
 */
#ifndef __MJ_UTIL__
#define __MJ_UTIL__

#include <iostream>
#include <unordered_map>
#include <map>
#include <vector>
#include "mj_define.h"

using namespace std;

//inline可以快一些，inline方法如果需要多文件调用，
//需要实现在头文件中，否则其他文件不可见
inline void
remove_nv(vector<uint8_t> &v, uint8_t value, uint8_t n) {
    int count = 0;
    for(auto it = v.begin(); it!=v.end();) {
        if(*it==value) {
            v.erase(it);
            count++;
            if(count>=n) 
                break;
        } else {
            it++;
        }
    }
}

inline void
catvv(vector<uint8_t> &v1, vector<uint8_t> &v2, int offset=-1) {
    v1.insert(v1.end(), v2.begin(), offset<0 ? v2.end() : v2.begin()+offset);
}

template <typename T, typename TT>
inline bool
findv(const vector<T> &v, TT value) {
    return find(v.begin(), v.end(), (T)value)!=v.end();
}

inline void 
supersplit(const string& s, vector<string>& v, const string& c) {
    std::string::size_type pos1, pos2;
    size_t len = s.length();
    pos2 = s.find(c);
    pos1 = 0;
    while(std::string::npos != pos2)
    {
        v.emplace_back(s.substr(pos1, pos2-pos1));
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != len)
        v.emplace_back(s.substr(pos1));
}

inline void
fxret2v(uint64_t r, vector<uint8_t> &ts) {
    MJ_FX_RET ret = {.retdata=r};
    for(int i=0; i<7; i++) {
        if(ret.data[i])
            ts.push_back(ret.data[i]);
        else
            break;
    }
}

inline uint64_t
fxv2ret(vector<uint8_t> &ts, int len, int8_t ret=1) {
    MJ_FX_RET mfret = {.retdata=0};
    mfret.ret = ret;
    for(int i=0; i<len; i++)
        mfret.data[i] = ts[i];
    return mfret.retdata;
}

inline uint64_t
fxv2ret(uint8_t v1, uint8_t v2=0, uint8_t v3=0, uint8_t v4=0, int8_t ret=1) {
    MJ_FX_RET mfret = {.retdata=0};
    mfret.ret = ret;
    mfret.data[0] = v1;
    mfret.data[1] = v2;
    mfret.data[2] = v3;
    mfret.data[3] = v4;
    return mfret.retdata;
}

#endif
